package com.itheima.day15.TCP.test6;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerDemo {
    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(10007);

        lo:
        while(true){
            Socket accept = ss.accept();

            // 接收数据，保存到本地
            InputStream is = accept.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            BufferedWriter bw = new BufferedWriter(new FileWriter("backBurner\\a\\a.txt"));
            String str;
            while((str = br.readLine()) != null){
                if("886".equals(str))
                    break;

                System.out.println(str);
                bw.write(str);
                bw.flush();
            }

            if("886".equals(str)){
                OutputStream os = accept.getOutputStream();
                bw = new BufferedWriter(new OutputStreamWriter(os));
                bw.write("服务器关闭");
                bw.newLine();
                bw.flush();
                accept.shutdownOutput();
                break ;
            }
            OutputStream os = accept.getOutputStream();
            bw = new BufferedWriter(new OutputStreamWriter(os));
            bw.write("录入成功");
            bw.newLine();
            bw.flush();

            bw.close();
            accept.close();
        }
        ss.close();
    }
}
